﻿<Window x:Class="ChessPlatform.UI.Desktop.GameWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ChessPlatform.UI.Desktop"
        xmlns:commands="clr-namespace:ChessPlatform.UI.Desktop.Commands"
        xmlns:converters="clr-namespace:ChessPlatform.UI.Desktop.Converters"
        xmlns:viewModels="clr-namespace:ChessPlatform.UI.Desktop.ViewModels"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Width="1020" Height="600" MinWidth="1020" MinHeight="600" WindowStartupLocation="CenterScreen" Margin="0"
        WindowState="Maximized" x:ClassModifier="internal">
    <Window.Resources>
        <converters:BooleanToVisibilityConverter x:Key="IsComputerPlayerActiveConverter" />
        <converters:KeyGestureConverter x:Key="KeyGestureConverter" />
        <system:Boolean x:Key="DebugShowGridLines">False</system:Boolean>

        <Style TargetType="Image" x:Key="NavigationItemImageStyle">
            <Setter Property="Opacity" Value="1" />
            <Setter Property="Effect" Value="{x:Null}" />
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Opacity" Value="0.5" />
                    <Setter Property="Effect">
                        <Setter.Value>
                            <BlurEffect KernelType="Gaussian" Radius="5" RenderingBias="Quality" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="{x:Static commands:GameWindowCommands.Exit}" Executed="Exit_Executed" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.NewGame}" Executed="NewGame_Executed" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.UndoLastMove}" Executed="UndoLastMove_Executed"
                CanExecute="UndoLastMove_CanExecute" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.CopyFenToClipboard}"
                Executed="CopyFenToClipboard_Executed" CanExecute="CopyFenToClipboard_CanExecute" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.CopyHistoryToClipboard}"
                Executed="CopyHistoryToClipboard_Executed" CanExecute="CopyHistoryToClipboard_CanExecute" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.ReversedBoardView}"
                Executed="ReversedBoardView_Executed" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.ShowPlayerFeedback}"
                Executed="ShowPlayerFeedback_Executed" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.ShowPlayersTimers}"
                Executed="ShowPlayersTimers_Executed" />
        <CommandBinding Command="{x:Static commands:GameWindowCommands.RequestMoveNow}"
                Executed="RequestMoveNow_Executed" CanExecute="RequestMoveNow_CanExecute" />
    </Window.CommandBindings>

    <Window.TaskbarItemInfo>
        <TaskbarItemInfo x:Name="TaskbarItemInfoInstance" />
    </Window.TaskbarItemInfo>

    <Window.DataContext>
        <viewModels:GameWindowViewModel x:Name="ViewModel" />
    </Window.DataContext>

    <Grid Name="MainGrid" Margin="5" ShowGridLines="{StaticResource DebugShowGridLines}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="320" />
        </Grid.ColumnDefinitions>

        <Menu Name="MainMenu" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch"
                VerticalAlignment="Top" Focusable="False">
            <Menu.Resources>
                <Style TargetType="Image" BasedOn="{StaticResource NavigationItemImageStyle}" />
            </Menu.Resources>

            <MenuItem Header="_Game">
                <MenuItem Command="{x:Static commands:GameWindowCommands.NewGame}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}">
                    <MenuItem.Icon>
                        <Image
                                Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/NewGame.Menu.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Command="{x:Static commands:GameWindowCommands.RequestMoveNow}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}">
                    <MenuItem.Icon>
                        <Image
                                Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/MoveNow.Menu.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Command="{x:Static commands:GameWindowCommands.Exit}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}">
                    <MenuItem.Icon>
                        <Image
                                Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/Exit.Menu.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="_History">
                <MenuItem Command="{x:Static commands:GameWindowCommands.UndoLastMove}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}">
                    <MenuItem.Icon>
                        <Image
                                Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/UndoLastMove.Menu.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Command="{x:Static commands:GameWindowCommands.CopyFenToClipboard}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}" />
                <MenuItem Command="{x:Static commands:GameWindowCommands.CopyHistoryToClipboard}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}" />
            </MenuItem>
            <MenuItem Header="_View">
                <MenuItem Command="{x:Static commands:GameWindowCommands.ShowPlayerFeedback}"
                        IsChecked="{Binding ShouldShowPlayerFeedback}" IsCheckable="True"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}" />
                <MenuItem Command="{x:Static commands:GameWindowCommands.ShowPlayersTimers}"
                        IsChecked="{Binding ShouldShowPlayersTimers}" IsCheckable="True"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}" />
                <Separator />
                <MenuItem Command="{x:Static commands:GameWindowCommands.ReversedBoardView}"
                        IsChecked="{Binding IsReversedView}"
                        InputGestureText="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource KeyGestureConverter}}" />
            </MenuItem>
        </Menu>

        <ToolBar Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch"
                HorizontalContentAlignment="Stretch">
            <ToolBar.Resources>
                <Style TargetType="Image" BasedOn="{StaticResource NavigationItemImageStyle}" />
            </ToolBar.Resources>
            <Button Command="{x:Static commands:GameWindowCommands.NewGame}" ToolTip="New Game...">
                <Image Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/NewGame.Toolbar.png" />
            </Button>
            <Button Command="{x:Static commands:GameWindowCommands.RequestMoveNow}" ToolTip="Move Now">
                <Image Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/MoveNow.Toolbar.png" />
            </Button>
            <Separator />
            <Button Command="{x:Static commands:GameWindowCommands.UndoLastMove}" ToolTip="Undo Last Move...">
                <Image
                        Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/UndoLastMove.Toolbar.png" />
            </Button>
            <Separator />
            <ToggleButton Command="{x:Static commands:GameWindowCommands.ShowPlayerFeedback}"
                    ToolTip="Show Player Feedback"
                    IsChecked="{Binding Path=ShouldShowPlayerFeedback, Mode=OneWay}">
                <Image
                        Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/ShowPlayerFeedback.Toolbar.png" />
            </ToggleButton>
            <ToggleButton Command="{x:Static commands:GameWindowCommands.ShowPlayersTimers}" ToolTip="Show Timers"
                    IsChecked="{Binding Path=ShouldShowPlayersTimers, Mode=OneWay}">
                <Image
                        Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/ShowTimers.Toolbar.png" />
            </ToggleButton>
            <Separator />
            <ToggleButton Command="{x:Static commands:GameWindowCommands.ReversedBoardView}"
                    ToolTip="Reversed Board View"
                    IsChecked="{Binding Path=IsReversedView, Mode=OneWay}">
                <Image
                        Source="pack://application:,,,/ChessPlatform.UI.Desktop;component/Resources/ReversedBoardView.Toolbar.png" />
            </ToggleButton>
        </ToolBar>

        <Label Name="StatusLabel" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" FontWeight="Bold"
                Foreground="DarkMagenta" />

        <Viewbox Name="BoardViewbox" Grid.Row="3" Grid.Column="0" Margin="0" HorizontalAlignment="Stretch"
                VerticalAlignment="Top" Stretch="Uniform" StretchDirection="Both">
            <Grid ShowGridLines="{StaticResource DebugShowGridLines}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Border BorderBrush="Transparent" BorderThickness="2" Grid.Row="0" Grid.Column="0" Margin="0">
                    <Grid ShowGridLines="{StaticResource DebugShowGridLines}" HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="2*" />
                        </Grid.ColumnDefinitions>

                        <Grid.Resources>
                            <converters:BooleanToVisibilityConverter x:Key="ShowItemVisibilityConverter"
                                    TrueValue="Visible" FalseValue="Hidden" />
                        </Grid.Resources>

                        <TextBlock Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"
                                Text="{x:Static local:UIHelper.KingSymbol}"
                                Foreground="{Binding UpperPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Center" TextAlignment="Left" FontSize="12" />

                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding UpperPlayerTitle}"
                                Foreground="{Binding UpperPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Center" TextAlignment="Right" FontSize="5" />

                        <Grid Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Text="{Binding UpperPlayerLastMoveElapsed}"
                                    Visibility="{Binding ShouldShowPlayersTimers,Converter={StaticResource ShowItemVisibilityConverter}}"
                                    Foreground="{Binding UpperPlayerBrush}" HorizontalAlignment="Stretch"
                                    VerticalAlignment="Center" TextAlignment="Left" FontSize="3" />

                            <Separator Grid.Column="1" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />

                            <TextBlock Grid.Column="2" Text="{Binding UpperPlayerTotalElapsed}"
                                    Visibility="{Binding ShouldShowPlayersTimers,Converter={StaticResource ShowItemVisibilityConverter}}"
                                    Foreground="{Binding UpperPlayerBrush}" HorizontalAlignment="Stretch"
                                    VerticalAlignment="Center" TextAlignment="Right" FontSize="3" />
                        </Grid>

                        <TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding UpperPlayerPieceAdvantage}"
                                Foreground="{Binding UpperPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Top" TextAlignment="Left" FontSize="7" TextWrapping="Wrap" />

                        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding UpperPlayerFeedback}"
                                Visibility="{Binding ShouldShowPlayerFeedback,Converter={StaticResource ShowItemVisibilityConverter}}"
                                Foreground="{Binding UpperPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Top" TextAlignment="Left" FontSize="3" TextWrapping="Wrap" />

                        <TextBlock Grid.Row="3" Grid.Column="0" Text="{Binding LowerPlayerPieceAdvantage}"
                                Foreground="{Binding LowerPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Bottom" TextAlignment="Left" FontSize="7" TextWrapping="Wrap" />

                        <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding LowerPlayerFeedback}"
                                Visibility="{Binding ShouldShowPlayerFeedback,Converter={StaticResource ShowItemVisibilityConverter}}"
                                Foreground="{Binding LowerPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Bottom" TextAlignment="Left" FontSize="3" TextWrapping="Wrap" />

                        <TextBlock Grid.Row="4" Grid.Column="0" Grid.RowSpan="2"
                                Text="{x:Static local:UIHelper.KingSymbol}"
                                Foreground="{Binding LowerPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Center" TextAlignment="Left" FontSize="12" />

                        <TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding LowerPlayerTitle}"
                                Foreground="{Binding LowerPlayerBrush}" HorizontalAlignment="Stretch"
                                VerticalAlignment="Center" TextAlignment="Right" FontSize="5" />

                        <Grid Grid.Row="5" Grid.Column="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Text="{Binding LowerPlayerLastMoveElapsed}"
                                    Visibility="{Binding ShouldShowPlayersTimers,Converter={StaticResource ShowItemVisibilityConverter}}"
                                    Foreground="{Binding LowerPlayerBrush}" HorizontalAlignment="Stretch"
                                    VerticalAlignment="Center" TextAlignment="Left" FontSize="3" />

                            <Separator Grid.Column="1" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />

                            <TextBlock Grid.Column="2" Text="{Binding LowerPlayerTotalElapsed}"
                                    Visibility="{Binding ShouldShowPlayersTimers,Converter={StaticResource ShowItemVisibilityConverter}}"
                                    Foreground="{Binding LowerPlayerBrush}" HorizontalAlignment="Stretch"
                                    VerticalAlignment="Center" TextAlignment="Right" FontSize="3" />
                        </Grid>
                    </Grid>
                </Border>

                <Border BorderBrush="Transparent" BorderThickness="0,2,0,2" Grid.Row="0" Grid.Column="1" Margin="0">
                    <Grid Name="RankSymbolGrid" Margin="0" HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch" ShowGridLines="{StaticResource DebugShowGridLines}" />
                </Border>

                <Border Name="BoardGridBorder" BorderBrush="Black" BorderThickness="2" Grid.Row="0" Grid.Column="2"
                        Margin="0">
                    <Grid ShowGridLines="{StaticResource DebugShowGridLines}">
                        <Grid Name="BoardGrid" Margin="0" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch" ShowGridLines="{StaticResource DebugShowGridLines}" />

                        <Grid Name="PromotionContainerGrid" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch" Grid.ZIndex="10000" Background="Gray" Opacity="0.5" />

                        <Grid Name="ComputerThinkingGrid" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch" Grid.ZIndex="20000" Background="Gray" Opacity="0.3"
                                Visibility="{Binding IsComputerPlayerActive,Converter={StaticResource ResourceKey=IsComputerPlayerActiveConverter}}" />
                    </Grid>
                </Border>

                <Border BorderBrush="Transparent" BorderThickness="2,0,2,0" Grid.Row="1" Grid.Column="2" Margin="0">
                    <Grid Name="FileSymbolGrid" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                            ShowGridLines="{StaticResource DebugShowGridLines}" />
                </Border>
            </Grid>
        </Viewbox>

        <ScrollViewer Grid.Row="3" Grid.Column="1" VerticalScrollBarVisibility="Auto"
                HorizontalScrollBarVisibility="Auto" Focusable="False">
            <TextBlock Text="{Binding MoveHistory, Mode=OneWay}" Margin="5,0,5,0" Focusable="False"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left"
                    TextWrapping="WrapWithOverflow" FontSize="12" FontFamily="Courier New"
                    Background="{x:Static SystemColors.ControlDarkBrush}"
                    Foreground="{x:Static SystemColors.HighlightTextBrush}" />
        </ScrollViewer>
    </Grid>
</Window>